/**
 * 存储localStorage
 */
const setStore = (name, content) => {
  if (!name) {
    return
  }
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
  if (!name) return;
  return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
}


/**
 * 将 options.body 转化为对象,此处的 value 全部为 string 类型
 */
const formToJson = formStr=> {
  let arr = formStr.split('&')
  let obj = {}
  arr.forEach(item => {
    let key = item.split('=')[0]
    let value = item.split('=')[1]
    obj[key] = value
  })
  return obj
}



export default {
  // Vue.js的插件应当有一个公开方法 install。这个方法的第一个参数是 Vue 构造器，第二个参数是一个可选的选项对象。
  install: function(Vue) {
    Vue.prototype.$setStore = (name, content) => setStore(name, content)
    Vue.prototype.$getStore = (name) => getStore(name)
    Vue.prototype.$removeStore = (name) => removeStore(name)
    Vue.prototype.$formToJson = (formStr) => formToJson(formStr)
  }
}
